package com.bingo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingo.dao.HisPoolMapper;
import com.bingo.entity.HisPool;
import com.bingo.entity.CurPool;
import com.bingo.service.HisPoolService;
import com.bingo.service.CurPoolService;
import com.bingo.service.SiteService;
import com.bingo.until.IsToday;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;


@Service
public class HisPoolServiceImpl extends ServiceImpl<HisPoolMapper, HisPool> implements HisPoolService {

    private CurPoolService curPoolService;
    @Autowired
    public void setPrePoolService(CurPoolService curPoolService) {
        this.curPoolService = curPoolService;
    }

    private SiteService siteService;
    @Autowired
    public void setSiteService(SiteService siteService) {
        this.siteService = siteService;
    }


    @Override
    public HisPool getPoolByNo(String poolNo) {
        QueryWrapper<HisPool> wrapper = new QueryWrapper<>();
        wrapper.eq("pool_no", poolNo);
        HisPool hisPool = this.getOne(wrapper);
        return hisPool;
    }

    @Override
    public HisPool getPoolByNoAndDate(String poolNo, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = sdf.format(date);
        QueryWrapper<HisPool> wrapper = new QueryWrapper<>();
        wrapper.eq("pool_no", poolNo);
        wrapper.eq("date", dateString);
        HisPool hisPool = this.getOne(wrapper);
        return hisPool;
    }

    @Override
    public HisPool getPrePoolByNo(String poolNo) {

        return null;
    }

    @Override
    public void updatePool(HisPool hisPool) {
        if (DateUtils.isSameDay(hisPool.getDate(),new Date(System.currentTimeMillis()))){
            //同一天，直接修改
            this.updateById(hisPool);
        }else {
            //修改上一条状态
            UpdateWrapper<HisPool> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", hisPool.getId()).set("is_new", 0);
            this.update(updateWrapper);
            //插入一条新的
            hisPool.setId(null);
            hisPool.setDate(new Date(System.currentTimeMillis()));
            this.save(hisPool);
        }
    }


    @Override
    public void syncCurPoolToHisPool() {
        List<CurPool> list = curPoolService.list();
        for (CurPool curPool : list) {
            HisPool hisPool = new HisPool();
            hisPool.setPoolNo(curPool.getPoolNo());
            hisPool.setSiteNo(curPool.getSiteNo());
            hisPool.setQuantity(curPool.getQuantity());
            hisPool.setWeight(curPool.getWeight());
            hisPool.setDate(curPool.getDate());
            hisPool.setType(curPool.getType());
            hisPool.setArea(curPool.getArea());
            this.save(hisPool);
        }
        for (CurPool curPool : list) {
            //修改日期为当天
            curPool.setDate(new Date(System.currentTimeMillis()));
            curPoolService.updateById(curPool);
        }
    }

    @Override
    public List<HisPool> getPoolStats(List<String> poolNos, Date date) {

        //未提供塘号
        if (poolNos == null || poolNos.size() == 0){
            poolNos = curPoolService.getPoolNos();
        }

        //如果仍然为空
        if (poolNos == null || poolNos.size() == 0){
            return null;
        }

        List<HisPool> hisPools = new LinkedList<>();

        if (IsToday.isToday(date)){
            QueryWrapper<CurPool> wrapper = new QueryWrapper<>();
            wrapper.in("pool_no", poolNos);
            wrapper.eq("date", date);
            List<CurPool> list = curPoolService.list(wrapper);
            for (CurPool curPool : list) {
                HisPool hisPool = new HisPool();
                BeanUtils.copyProperties(curPool, hisPool);
                hisPools.add(hisPool);
            }
        }else {
            QueryWrapper<HisPool> wrapper = new QueryWrapper<>();
            wrapper.in("pool_no", poolNos);
            wrapper.eq("date", date);
            hisPools = this.list(wrapper);
        }
        return hisPools;
    }
}
